/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import DAO.EspacoDAO;
import Model.Espaco;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;

/**
 *
 * @author 0433101
 */
public class validaEspaco extends Processor {

    @Override
    public void execute() throws ServletException, IOException {
        PrintWriter out = getResponse().getWriter();
        String n = getRequest().getParameter("numero");
        String e = getRequest().getParameter("endereco");
        int numero = -1;
        int endereco = -1;
        
        try{
            numero = Integer.parseInt(n);
        }
        catch(NumberFormatException ex){
            out.println("<font color='red'> Número inválido! </color>");
        }
        
        try{
            endereco = Integer.parseInt(e);
        }
        catch(NumberFormatException ex){
            throw new ServletException("Endereco have invalid number!");
        }
        try {
            Espaco espaco = EspacoDAO.PesquisarByNumEndereco(numero, endereco);
            
            if(espaco == null){
                out.println("<font color='green'> Espaço válido! </color>");
            }
            else{
                out.println("<font color='red'> Espaço já existe! </color>");
            }
        } catch (SQLException ex) {
            throw new ServletException(ex);
        } catch (ClassNotFoundException ex) {
            throw new ServletException(ex);
        }
    }
    
}
